﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using Rodunu.Business.Interfaces;
using Rodunu.Services.Amazon;
using Rodunu.Business.Models.Movies;
using Rodunu.Clients.WPF.Search;
using Rodunu.Services.Interfaces;

namespace Rodunu.Clients.WPF
{
	/// <summary>
	/// Interaction logic for AmazonSearch.xaml
	/// </summary>
	public partial class AmazonSearch : ISearchControl
    {
        private readonly string _searchFor;
        private readonly BackgroundWorker _backgroundWorker;
        private readonly StackPanel _moviesInAmazon;
        private readonly IAmazonService _amazonService;
	    private double _height;
	    private readonly ScrollViewer _scrollViewer;

        public AmazonSearch(string searchFor, double height)
        {
            InitializeComponent();

            _scrollViewer = new ScrollViewer();
            _height = height;
            _amazonService = new AmazonService();

            _searchFor = searchFor;

            _backgroundWorker = new BackgroundWorker();
            _backgroundWorker.DoWork += _backgroundWorker_DoWork;
            _backgroundWorker.RunWorkerCompleted += _backgroundWorker_RunWorkerCompleted;
            _backgroundWorker.RunWorkerAsync(new SearchArguments(_searchFor, 0, 0, true));

            _moviesInAmazon = new StackPanel();
            _moviesInAmazon.Margin = new Thickness(4, 0, 2, 0);

        }

        bool firstTime = true;

        void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            var amazonSearchResult = (AmazonSearchResult)e.Result;

            if (firstTime)
            {
                _scrollViewer.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                _scrollViewer.Content = _moviesInAmazon;
                _scrollViewer.Margin = new Thickness(0, 2, -5, 0);

                LayoutRoot.Children.Add(_scrollViewer);
                Grid.SetRow(_scrollViewer, 1);
                firstTime = false;

                LayoutRoot.Children.Remove(Loader);
            }

            foreach (var item in amazonSearchResult.Titles)
            {
                _moviesInAmazon.Children.Add(new SearchResultButton(item.Name, Convert.ToInt32(item.Id)));
            }
            NumberOfResults.Content = _moviesInAmazon.Children.Count;

            if (amazonSearchResult.TotalPages - amazonSearchResult.PageNumber > 0)
            {
                _backgroundWorker.RunWorkerAsync(new SearchArguments(_searchFor, amazonSearchResult.TotalPages, amazonSearchResult.PageNumber + 1, false));
            }

            if (_height > 30)
                _scrollViewer.Height = _height - 40;
            else
                _scrollViewer.Height = 40;
        }

        void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            var searchArguments = (SearchArguments)e.Argument;
            e.Result = _amazonService.SearchWebService(searchArguments.SearchFor, searchArguments.PageNumber,
                 searchArguments.TotalPages, searchArguments.FirstRun);
        }

        #region ISearchControl Members

        public void SetHeight(double height)
        {
            _height = height;
            UserControl.Height = _height;
            
            if (_height > 30)
                _scrollViewer.Height = _height - 40;
            else
                _scrollViewer.Height = 40;
        }

        #endregion
    }

    class SearchArguments
    {
        public string SearchFor { get; set; }
        public int TotalPages { get; set; }
        public int PageNumber { get; set; }
        public bool FirstRun { get; set; }

        public SearchArguments(string searchFor, int totalPages, int pageNumber, bool firstRun)
        {
            SearchFor = searchFor;
            TotalPages = totalPages;
            PageNumber = pageNumber;
            FirstRun = firstRun;
        }
    }
}